package com.atguigu.gulimall.product.service.impl;

import com.atguigu.gulimall.product.dao.AttrAttrgroupRelationDao;
import com.atguigu.gulimall.product.entity.AttrAttrgroupRelationEntity;
import com.atguigu.gulimall.product.entity.AttrEntity;
import com.atguigu.gulimall.product.service.AttrService;
import com.atguigu.gulimall.product.vo.AttrGroupRelationVo;
import com.atguigu.gulimall.product.vo.AttrGroupWitchattrVo;
import com.atguigu.gulimall.product.vo.AttrRespVo;
import com.atguigu.gulimall.product.vo.SpuItemAttrGroupVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;

import com.atguigu.gulimall.product.dao.AttrGroupDao;
import com.atguigu.gulimall.product.entity.AttrGroupEntity;
import com.atguigu.gulimall.product.service.AttrGroupService;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {
    @Autowired
    private AttrAttrgroupRelationDao attrAttrgroupRelationDao;
    @Autowired
    private AttrService attrService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 根据params查询条件查询，
     * 如果是全部查询，也需要模糊查询
     *
     * @param params
     * @param catelogId
     * @return
     */
    @Override
    public PageUtils queryPage(Map<String, Object> params, Long catelogId) {

//       如：key: '华为'//检索关键字
        String key = (String)params.get("key");
//        select * from pms_attr_group while catelog_id = ? and (attr_group_id = ? or attr_group_name like %?%)
        QueryWrapper<AttrGroupEntity> wrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(key)){
//            精确查询分组id，模糊查询组名
            wrapper.and((obj)->{
                obj.eq("attr_group_id",key).or().like("attr_group_name",key);
            });
        }

//        如果catelogId为0，说明查询全部
        if (catelogId == 0){
            IPage<AttrGroupEntity> page = this.page( new Query<AttrGroupEntity>().getPage(params), wrapper);
            return new PageUtils(page);
        }else{
//            根据分类查询本类下的所有产品
            wrapper.eq("catelog_id",catelogId);
            IPage<AttrGroupEntity> page = this.page(new Query<AttrGroupEntity>().getPage(params),wrapper);
            return new PageUtils(page);
        }
    }

    /**
     * 删除属性与分组的关联关系
     * @param attrGroupRelationVo
     */
    @Override
    public void deleteAttrGroupList(AttrGroupRelationVo[] attrGroupRelationVo) {
        /**
         * 删除属性与分组的关联关系只需要删除关联关系表就ok
         * 如果是批量删除，就需要很多次请求。所以为了解决这一问题，我们再dao层手写sql
         *  因为AttrGroupRelationVo中的两个id与实体类的一样。所以对象拷贝成实体传递
         */
        List<AttrAttrgroupRelationEntity> entities = Arrays.asList(attrGroupRelationVo).stream().map((item) -> {
            AttrAttrgroupRelationEntity relationEntity = new AttrAttrgroupRelationEntity();
            BeanUtils.copyProperties(item, relationEntity);
            return relationEntity;
        }).collect(Collectors.toList());

        attrAttrgroupRelationDao.deleteBatchRelation(entities);
    }

    /**
     * 根据三级分类id查询所有分组以及这些组中的属性
     * @param catelogId
     * @return
     */
    @Override
    public List<AttrGroupWitchattrVo> getAttrGroupWitchAttrsByCatelogId(Long catelogId) {
//        1、根据id查询分组信息集合
        List<AttrGroupEntity> attrGroupIds = this.list(new QueryWrapper<AttrGroupEntity>().eq("catelog_id", catelogId));

        List<AttrGroupWitchattrVo> collect = attrGroupIds.stream().map(item -> {
//            得到当前的每一条属性分组id
            AttrGroupWitchattrVo attrGroupRelationVo = new AttrGroupWitchattrVo();
            BeanUtils.copyProperties(item, attrGroupRelationVo);
//            获取属性分组的关联的所有属性
            List<AttrEntity> relationAttrs = attrService.getRelationAttr(item.getAttrGroupId());
            attrGroupRelationVo.setAttrs(relationAttrs);
            return attrGroupRelationVo;
        }).collect(Collectors.toList());

        return collect;
    }

    @Override
    public List<SpuItemAttrGroupVo> getAttrGroupWitchAttrsBySpuId(Long spuId,Long catalogId) {
//        1、查出当前spu对应的所有属性的分组信息以及当前分组下的所有属性对应的值
        AttrGroupDao baseMapper = this.getBaseMapper();
        List<SpuItemAttrGroupVo> vos = baseMapper.getAttrGroupWitchAttrsBySpuId(spuId,catalogId);
        return vos;
    }

}